$(function () { //adminLTE Table example JS
    $("#example1").DataTable();
    $('#jobdg').DataTable({
        "paging": true,
        "processing":true,
        "lengthChange": false,
        "searching": false,
        "ordering": true,
        "info": true,
        "autoWidth": false,
        "serverSide": true,
        "ajax":{
            "url":"JobManagement/getJobList"
        },
        "aoColumnDefs":[{
            "title":"",
            "targets":0,
            "sClass": "text-center",
            "data": function(item){ return  item;},
            "searchable":false,
            "orderable":false,
            "width": "5%",
            "render":function(data,type,row){
                return '<input id="checkchild" class="checkchild" type="checkbox"/>';
            }
        }],
        "columns":[
        {
            "data":""
        },{
            "data":"idJob"
        },{
            "data":"name"
        },{
            "data":"description"
        },{
            "data":"jobVersion"
        },{
            "data":"runStatus"
        }]
    });
});

function searchJob(){
    alert("Search Job");
}

$(function () { //adminLTE Form Elements example JS
    //Initialize Select2 Elements
    $(".select2").select2();

    //Datemask dd/mm/yyyy
    $("#datemask").inputmask("dd/mm/yyyy", {"placeholder": "dd/mm/yyyy"});
    //Datemask2 mm/dd/yyyy
    $("#datemask2").inputmask("mm/dd/yyyy", {"placeholder": "mm/dd/yyyy"});
    //Money Euro
    $("[data-mask]").inputmask();

    //Date range picker
    $('#reservation').daterangepicker();
    //Date range picker with time picker
    $('#reservationtime').daterangepicker({timePicker: true, timePickerIncrement: 30, format: 'MM/DD/YYYY h:mm A'});
    //Date range as a button
    $('#daterange-btn').daterangepicker(
        {
          ranges: {
            'Today': [moment(), moment()],
            'Yesterday': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
            'Last 7 Days': [moment().subtract(6, 'days'), moment()],
            'Last 30 Days': [moment().subtract(29, 'days'), moment()],
            'This Month': [moment().startOf('month'), moment().endOf('month')],
            'Last Month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
          },
          startDate: moment().subtract(29, 'days'),
          endDate: moment()
        },
        function (start, end) {
          $('#daterange-btn span').html(start.format('MMMM D, YYYY') + ' - ' + end.format('MMMM D, YYYY'));
        }
    );

    //Date picker
    $('#datepicker').datepicker({
      autoclose: true
    });

    //iCheck for checkbox and radio inputs
    $('input[type="checkbox"].minimal, input[type="radio"].minimal').iCheck({
      checkboxClass: 'icheckbox_minimal-blue',
      radioClass: 'iradio_minimal-blue'
    });
    //Red color scheme for iCheck
    $('input[type="checkbox"].minimal-red, input[type="radio"].minimal-red').iCheck({
      checkboxClass: 'icheckbox_minimal-red',
      radioClass: 'iradio_minimal-red'
    });
    //Flat red color scheme for iCheck
    $('input[type="checkbox"].flat-red, input[type="radio"].flat-red').iCheck({
      checkboxClass: 'icheckbox_flat-green',
      radioClass: 'iradio_flat-green'
    });

    //Colorpicker
    $(".my-colorpicker1").colorpicker();
    //color picker with addon
    $(".my-colorpicker2").colorpicker();

    //Timepicker
    $(".timepicker").timepicker({
      showInputs: false
    });
});


$('#jobInfodlg').on("show.bs.modal",function(event){
    var button = $(event.relatedTarget);
    var title = button.data('title')
    $(this).find('.box-title').text(title);
});
function saveJobInfo() {
    var jobTable = $("#jobdg").dataTable();
    $('#jobInfodlg').modal('hide');
    $('#jobInfodlg').removeData();
    $("#jobInfoForm").ajaxSubmit(function(message) { 
        jobTable.api().ajax.reload();
        var jsonResult = eval('('+message+')');
        alert(jsonResult.message); 
    });
    return false;
}

function editConfiguration(){
    $('#configdlg').modal("show");
}

function scanLog(){
    $('#scanlogdlg').modal("show");
}

function getJobContent(){
    var jobTable = $("#jobdg").dataTable();
    var trList = jobTable.fnGetNodes();
    var jsonstr="[]";  
    var rows = eval('('+jsonstr+')');  
    for(var i = 0; i < trList.length; i++){
        var trObj = trList[i];
        if(trObj.firstElementChild.firstElementChild.checked){
              rows.push(jobTable.fnGetData(trObj));
        } 
    }
    if(rows.length!=0){
        if(rows.length==1){
            $.post(
                "JobManagement/getJobContent", 
                {
                    idJob : rows[0].idJob
                }, 
                function(result) {
                    $("#JobName").text(result.name);
                    $("#JobContent").text(result.sharedFile);
                    $('#contentdlg').modal("show");
                },
                "json"
            );
        }else {
            alert("Please choose only one row.");
        }
    }else{
        alert("Please choose one row first.");
    }
}

$('#noticedlg').on("show.bs.modal",function(event){
    var button = $(event.relatedTarget);
    var title = button.data('title');
    var msg = button.data('message');
    $("#noticeMessage").text(msg);
    $("#noticeTitle").text(title);
});
function NoticeSure(){
    $('#noticedlg').modal('hide');
    var jobTable = $("#jobdg").dataTable();
    var trList = jobTable.fnGetNodes();
    var jsonstr="[]";  
    var rows = eval('('+jsonstr+')');  
    for(var i = 0; i < trList.length; i++){
        var trObj = trList[i];
        if(trObj.firstElementChild.firstElementChild.checked){
              rows.push(jobTable.fnGetData(trObj));
        } 
    }
    if(rows.length!=0){
        if(rows.length==1){
            var operation = $("#noticeTitle").text();
            if(operation=='Delete'){
                $.post(
                    "JobManagement/deleteJob", 
                    {
                        idJob : rows[0].idJob
                    }, 
                    function(result) {
                        jobTable.api().ajax.reload();
                        alert(result.message); 
                    },
                    "json"
                );
            }else if(operation=='Start'){
                $.post(
                    "JobManagement/startJob", 
                    {
                        idJob : rows[0].idJob
                    }, 
                    function(result) {
                        jobTable.api().ajax.reload();
                        alert(result.message); 
                    },
                    "json"
                );
            }else if(operation=='Stop'){
                $.post(
                    "JobManagement/stopJob", 
                    {
                        idJob : rows[0].idJob
                    }, 
                    function(result) {
                        jobTable.api().ajax.reload();
                        alert(result.message); 
                    },
                    "json"
                );
            }
        }else {
            alert("Please choose only one row.");
        }
    }else{
        alert("Please choose one row first.");
    }
}


